package com.cncf.controller.manage;

import com.cncf.response.ResponseData;
import com.cncf.util.UploadFileUtil;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zj
 * @date 2017/11/2 14:57
 */
@Api(value = "Upload", description = "")
@Controller
@RequestMapping("/manage/upload")
public class ManageUploadController {

    private static final Logger logger = LoggerFactory.getLogger(ManageUploadController.class);

    private static String ARTICLE_IMG_ROOT = "/article/img/";
    private static String ARTICLE_ATTACH_ROOT = "/article/attach/";
    private static String REPORT_FILE_ROOT = "/report_file/";
    private static String EXCEL_FOR_IMPORT = "/excel_for_import/";

    public ResponseData<String> common(MultipartFile file,String url){
        ResponseData<String> responseData=new ResponseData<>();
        String imgUrl = UploadFileUtil.uploadFile(file,url);
        responseData.jsonFill(1,null,imgUrl);
        return responseData;
    }

    @ApiOperation(value = "上传新闻图片", notes = "")
    @RequestMapping(value = "uploadNewsImg", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<String> uploadNewsImg(@ApiParam("新闻图片") @RequestParam("img") MultipartFile img){
        String url=ARTICLE_IMG_ROOT;
        return common(img,url);
    }

    @ApiOperation(value = "上传新闻附件", notes = "")
    @RequestMapping(value = "uploadNewsAttach", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<String> uploadNewsAttach(@ApiParam("其它附件") @RequestParam("attach") MultipartFile attach){
        String url=ARTICLE_ATTACH_ROOT;
        return common(attach,url);
    }

/*    @ApiOperation(value = "上传检验报告文件", notes = "")
    @RequestMapping(value = "uploadReportFile", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<String> uploadReportFile(@ApiParam("报告文件") @RequestParam("reportFile") MultipartFile reportFile){
        String url=REPORT_FILE_ROOT;
        return common(reportFile,url);
    }*/

    @ApiOperation(value = "批量上传检验报告文件", notes = "")
    @RequestMapping(value = "uploadReportFile", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<List<String>> uploadReportFile(
            @ApiParam("报告文件列表") @RequestParam("reportFileList") List<MultipartFile> reportFileList){
        String parentPath=REPORT_FILE_ROOT;
        ResponseData<List<String>> responseData=new ResponseData<>();
        List<String> urlList=new ArrayList<>();
        for (MultipartFile file:reportFileList) {
            String url = UploadFileUtil.uploadFileKeepFileName(file,parentPath);
            urlList.add(url);
        }
        responseData.jsonFill(1,null,urlList);
        return responseData;
    }

    @ApiOperation(value = "上传excel", notes = "")
    @RequestMapping(value = "uploadExcel", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<String> uploadExcel(@ApiParam("excel文件") @RequestParam("excelFile") MultipartFile excelFile){
        String url=EXCEL_FOR_IMPORT;
        return common(excelFile,url);
    }

}
